MYOWD=/data/data/com.termux/files/home/gitee/emp
NetDir="$MYOWD/.cache"
mkdir -p $NetDir
source $MYOWD/base.sh
source $Config_Dir/163.sh
#geturl(){
#	bash $Config_Dir/163.sh geturl $1 $2 $3
#}
A="🍎"
main(){
while true;do
	Playlist=$(Get Playlist)

	num=$(lget num)
	mode=$(lget mode)
	case $mode in
		loop);;
		list)let num++ ;;
		shuffle)let num=$RANDOM ;;
	esac

	totalnum=$(lget totalnum)
	if [[ $totalnum -eq 0 ]];then
		echo "播放列表为空. 再见🍎"
		exit
	fi
	num=$(( num%totalnum ))
	[[ $num -lt 1 ]] && num=$totalnum

	info=$(sed -n ${num}p $Tmp_File.use)
	filename_abs=$(echo $info|Get info filename)
	filename=$(echo $info|Get info filename|sed 's#.*/##')
	id=$(echo $info|Get info id)
	mvid=$(echo $info|Get info 'mv')
	dir=$(lget dir) #指的是那一个目前去使用
	Type=$(echo $info|Get info 'type')
	Play=$(lget play)


	[[ $Type = 'mv' ]] && id=$mvid
	info="$Playlist, $num/$totalnum, $mode, $dir, '$filename', $play, $id"
	#echo $info

	lset num $num
	lset info "$info"
	lset filename "$filename"

	[[ $Play = 'all' ]] && Play=$Type
	case $Play in
		net)size=$(geturl 'size' $id)
			url=$(geturl 'song' $id $mvid) ;;
		mv)size=$(geturl 'mvsize' $mvid)
			url=$(geturl 'mv' $mvid) ;;
		flac|mp3)
			url="$filename_abs" ;;
	esac
	case $Play in
		net|mv)size="$(( size>>20 ))M" ; s="size:$size, url:$url";;
		flac|mp3)s=$(ls -hs "$filename_abs") size=${s%% *};;
	esac
	lset url "$url"
	lset size "$size"
	#echo "$s"

	lcat

	echo "$A $(lget info)"
	NDL="$NetDir/$Playlist"
	mkdir -p $NDL
	if [[ ! -f $NDL/"$filename"  ]];then
		n=$(ls "$NetDir"/*/*|grep "$filename"|sed -n 1p)
		if [[ -z "$n" && ! -f "$url" && ! -z "$url" ]];then

			echo "$A 源文件$url"
			echo "$A 下载..."
			wget -q  "$url" -O "$NDL/$filename" &
			# wget  "$url" -O "$NDL/$filename" &
			$Player_Cmd "$url"
		elif [[ -z "$n" && -f "$url" ]];then
			echo "$A 源文件$url"
			echo "$A 链接..."
			ln -s "$url" "$NDL"
			$Player_Cmd "$url"
			#$Player_Cmd "$NDL/$filename"
		elif [[ ! -z "$n" ]];then
			echo "$A 源文件$n"
			echo "$A 链接..."
			ln -s "$n" "$NDL"
			$Player_Cmd "$url"
		fi
	else
		echo "$A 本地缓存 $NDL/$filename"
		$Player_Cmd "$NDL/$filename"
	fi

	clear 
done
}
if [[ $1 = run ]];then
	main &
fi
